package gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.Color;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import java.awt.SystemColor;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JButton;
import javax.swing.JPasswordField;

import classes.ConnectionDB;

public class UpdateDetails extends JFrame {

	private JPanel contentPane;
	private JTextField userField;
	private JPasswordField oldPassField;
	private JPasswordField newPassField;
	private static JFrame frm;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					UpdateDetails frame = new UpdateDetails(null,null);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public UpdateDetails(final Properties langFile, final String userName) {
		frm = this;
		this.setExtendedState(JFrame.MAXIMIZED_BOTH);
		this.setUndecorated(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBackground(Color.DARK_GRAY);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);

		JLabel lblNewLabel = new JLabel(
				langFile.getProperty("usr.menu.update.details.title"));
		lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 27));
		lblNewLabel.setForeground(SystemColor.inactiveCaption);
		lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);

		JLabel lblUserName = new JLabel(langFile.getProperty("usr.name"));
		lblUserName.setFont(new Font("Tahoma", Font.BOLD, 15));
		lblUserName.setForeground(SystemColor.activeCaptionText);

		userField = new JTextField();
		userField.setColumns(10);

		JButton btnChangePassword = new JButton(
				langFile.getProperty("usr.menu.update.details.changePassword"));

		final JLabel lblOldPassword = new JLabel(
				langFile.getProperty("usr.menu.update.details.oldPassLbl"));
		lblOldPassword.setFont(new Font("Tahoma", Font.BOLD, 15));
		lblOldPassword.setForeground(SystemColor.activeCaptionText);

		final JLabel lblNewPassword = new JLabel(
				langFile.getProperty("usr.menu.update.details.newPassLbl"));
		lblNewPassword.setFont(new Font("Tahoma", Font.BOLD, 15));
		lblNewPassword.setForeground(SystemColor.activeCaptionText);

		JButton btnSave = new JButton(
				langFile.getProperty("usr.menu.update.details.save"));

		JButton btnCancel = new JButton(
				langFile.getProperty("usr.menu.update.details.cancel"));

		oldPassField = new JPasswordField();
		oldPassField.setColumns(15);

		newPassField = new JPasswordField();
		newPassField.setColumns(15);

		lblNewPassword.setVisible(false);
		lblOldPassword.setVisible(false);
		oldPassField.setVisible(false);
		newPassField.setVisible(false);

		btnChangePassword.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				lblNewPassword.setVisible(true);
				lblOldPassword.setVisible(true);
				oldPassField.setVisible(true);
				newPassField.setVisible(true);
			}
		});

		btnCancel.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				frm.setVisible(false);

			}
		});

		btnSave.addActionListener(new ActionListener() {

			@SuppressWarnings("deprecation")
			@Override
			public void actionPerformed(ActionEvent e) {
				JFrame frame = new JFrame(
						"JOptionPane showMessageDialog example");
				final Connection connection = new ConnectionDB()
						.createConnection();
				java.sql.PreparedStatement statement;
				
					if(!userField.getText().isEmpty())
					{
						try {
							statement = connection.prepareStatement("SELECT users_name FROM USERS WHERE users_name  = ?");
							statement.setString(1,userField.getText());
							ResultSet result = statement.executeQuery();
							if(result.next())
							{
								JOptionPane.showMessageDialog(
										frame,
										langFile.getProperty("usr.menu.update.details.aler.userexist"));
							}
						} catch (SQLException e1) {
							e1.printStackTrace();
						}	
					}
				if (userField.getText().isEmpty()
						&& (oldPassField.getText().isEmpty() || newPassField
								.getText().isEmpty())) {
					JOptionPane.showMessageDialog(
							frame,
							langFile.getProperty("usr.menu.update.details.alert.noFieldsInserted"));
				}

				else {
					if ((oldPassField.getText().isEmpty() && newPassField
							.getText().isEmpty())) {
						
								if (JOptionPane.showConfirmDialog(frame,
										langFile.getProperty("usr.menu.file.addTopic.alert.replace")+" "
												+ userName + " " +langFile.getProperty("usr.menu.file.addTopic.alert.with")
												+" "+ userField.getText() + " ?",
										"Alert", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
								try {
									statement = connection
											.prepareStatement("UPDATE USERS SET users_name = ? WHERE users_name = ?");
									statement.setString(1,userField.getText());
									statement.setString(2,userName);
									statement.executeUpdate();
									JOptionPane.showMessageDialog(
											frame,
											langFile.getProperty("usr.menu.update.details.alert.detailsUpdated"));
									userField.setText("");
								} catch (SQLException e1) {
									e1.printStackTrace();
								}
								}
							}
					
					else{
						if ((oldPassField.getText().isEmpty() || newPassField
								.getText().isEmpty())) {
							JOptionPane.showMessageDialog(
									frame,
									langFile.getProperty("usr.menu.update.details.alert.passwordError"));
						}
						else
						{
							if(!userField.getText().isEmpty()){
								if (JOptionPane.showConfirmDialog(frame,
										langFile.getProperty("usr.menu.file.addTopic.alert.replace")+" "
												+ userName + " " +langFile.getProperty("usr.menu.file.addTopic.alert.with")
												+" "+ userField.getText() + " ?",
										"Alert", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
								try {
									statement = connection
											.prepareStatement("UPDATE USERS SET users_name = ?,users_password = ? WHERE users_name = ?");
									statement.setString(1,userField.getText());
									statement.setString(2,newPassField.getText());
									statement.setString(3,userName);
									statement.executeUpdate();
									JOptionPane.showMessageDialog(
											frame,
											langFile.getProperty("usr.menu.update.details.alert.detailsUpdated"));
									userField.setText("");
									newPassField.setText("");
									oldPassField.setText("");
									
								} catch (SQLException e1) {
									e1.printStackTrace();
								}
								}
							}
							else
							{
								try {
									statement = connection
											.prepareStatement("UPDATE USERS SET users_password = ? WHERE users_name = ?");
									statement.setString(1,newPassField.getText());
									statement.setString(2,userName);
									statement.executeUpdate();
								} catch (SQLException e1) {
									e1.printStackTrace();
								}
							}
						}
					}
				}
			}
		});

		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane
				.setHorizontalGroup(gl_contentPane
						.createParallelGroup(Alignment.TRAILING)
						.addGroup(
								gl_contentPane
										.createSequentialGroup()
										.addContainerGap()
										.addComponent(lblNewLabel,
												GroupLayout.DEFAULT_SIZE, 422,
												Short.MAX_VALUE))
						.addGroup(
								gl_contentPane
										.createSequentialGroup()
										.addGap(105)
										.addGroup(
												gl_contentPane
														.createParallelGroup(
																Alignment.LEADING)
														.addComponent(
																btnChangePassword)
														.addGroup(
																gl_contentPane
																		.createSequentialGroup()
																		.addGroup(
																				gl_contentPane
																						.createParallelGroup(
																								Alignment.LEADING)
																						.addComponent(
																								lblUserName)
																						.addComponent(
																								lblOldPassword)
																						.addComponent(
																								lblNewPassword))
																		.addGap(21)
																		.addGroup(
																				gl_contentPane
																						.createParallelGroup(
																								Alignment.TRAILING,
																								false)
																						.addComponent(
																								newPassField)
																						.addComponent(
																								oldPassField,
																								Alignment.LEADING)
																						.addComponent(
																								userField,
																								Alignment.LEADING,
																								GroupLayout.DEFAULT_SIZE,
																								158,
																								Short.MAX_VALUE))))
										.addContainerGap(29, Short.MAX_VALUE))
						.addGroup(
								gl_contentPane.createSequentialGroup()
										.addContainerGap(178, Short.MAX_VALUE)
										.addComponent(btnSave).addGap(18)
										.addComponent(btnCancel).addGap(114)));
		gl_contentPane
				.setVerticalGroup(gl_contentPane
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								gl_contentPane
										.createSequentialGroup()
										.addContainerGap()
										.addComponent(lblNewLabel)
										.addGap(39)
										.addGroup(
												gl_contentPane
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(
																lblUserName)
														.addComponent(
																userField,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE))
										.addGap(29)
										.addComponent(btnChangePassword)
										.addGap(37)
										.addGroup(
												gl_contentPane
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(
																lblOldPassword)
														.addComponent(
																oldPassField,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE))
										.addGap(24)
										.addGroup(
												gl_contentPane
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(
																lblNewPassword)
														.addComponent(
																newPassField,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												ComponentPlacement.RELATED)
										.addGroup(
												gl_contentPane
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(btnCancel)
														.addComponent(btnSave))
										.addContainerGap(
												GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)));
		contentPane.setLayout(gl_contentPane);
	}
}
